<?php
namespace app\common\bonus;

Class Score extends Common
{
	static $bonus_name='score';

	public function handle($data){
		$actionData = $data['action_data'];
		$reason_time = $data['create_time'];
		$score_id = $data['operate_id'];

		$uid = $actionData['uid'];
		$score = $actionData['score'];
		$reason_day = date("Ymd",$reason_time);
		$type = $actionData['type'];

        $memberScoreRecordModel = model('member_score_record');
        $memberModel = model('member');
        $memberScoreDailyModel = model('member_score_daily');

        $seters = model('member_seter')->getSetersByUid($uid);
        $seters = array_column($seters,'seter_uid');
        $seters[] = $uid;

        $memberModel->startTrans();
        try{
	        // 增加所有安置人与uid本身业绩
	        foreach($seters as $seter):
	            // 业绩明细记录
	            $remark = '用户['.$uid.']'.$type.'产生业绩';
	            $record[] = [
	                'score_id' => $score_id,
	                'reason_uid' => $uid,
	                'effect_uid' => $seter,
	                'score' => $score,
	                'remark'=> $remark,
	                'reason_time' => $reason_time,
	                'reason_day' => $reason_day,
	                'create_time' => time(),
	            ];

	            // 累计团队业绩
	            $update = [
	                'score_total'=>['exp', 'score_total+'.$score],
	            ];
	            $memberModel->where('id', $seter)->update($update);

	            // 累计日新增业绩
	            $memberScoreDailyModel->incDayScore($seter,$reason_day,$score);
	        endforeach;
	        // 业绩记录 
	        $memberScoreRecordModel -> saveAll($record);
	        model('member_score')->where('id'=>$score_id)->update(['status'=>1]);

	        $memberModel->commit();
	    }catch(\Exception $e){
	    	$memberModel->rollback();
	    	$this->error=$e->getMessage();
	    	return false;
	    }
	    return true;
	}

}